/*
build_code.dm
Provides the backbone to the building system, as well as some simple vars and procs, in addition to the turf
declarations.

Included
	deletion 			delete this item
	save mode modifier	save this item based on it's mode of save and the modifier to that mode
	update x			update this item based on x
*/

obj/builditems
	mouse_opacity=2
	layer=TURF_LAYER+1

	var
		aname				//the name you see when you look at it in the build list
		builtowner			//who built it
		path				//the path to the item, stored as text, used for clicking
		placeable=TRUE		//it can be placed
		saveable=TRUE		//it can be saved
		savex				//where it was saved, x
		savey
		savez
		uid					//it's uid, 1,10000000000000
		mesage=""			//for signs, the message, since you can't use message
		Tele_X=2			//for portals, where it teleports to, x
		Tele_Y=2
		Tele_Z=1
		iconFile			//custom icons
		password=""			//password for doors

	//olist is for the clipboard
	//owner is who built it
	New(loc,owner,olist,icon,iconstate,iconfile,name)
		if(olist)
			if(locate(type) in olist)
				var/obj/builditems/b = locate(type) in olist
				olist -= b
				olist += src
			else olist+=src
		..()
		if(icon)src.icon=icon
		if(iconstate)icon_state=iconstate
		if(iconfile)iconFile=iconfile
		if(name)src.name=name
		if(aname)src.name=aname
		if(!src.name)src.name="NO NAME"
		path="[type]"
		if(isturf(loc))
			//name=aname
			builtowner=owner
			savex=x
			savey=y
			savez=z

			uid=gen_uid(name)
			//making sure objects can't build themselves
			if(owner && owner!="the [ckey(name)]")
				var/obj/builditems/new_item = src
				//saving the object
				spawn(50)
					Query("INSERT INTO `[sql_objecttable]` (x,y,z,typepath,builtowner,uid,density,luminosity,invisibility,opacity,mesage,Tele_X,Tele_Y,Tele_Z,layer,infra_luminosity,icon_state,iconFile,name) VALUES \
																							  ([new_item.x],[new_item.y],[new_item.z],'[new_item.type]','[new_item.builtowner]',[new_item.uid],[new_item.density],[new_item.luminosity],[new_item.invisibility],[new_item.opacity],'[new_item.mesage]',[new_item.Tele_X],[new_item.Tele_Y],[new_item.Tele_Z],[new_item.layer],[new_item.infra_luminosity],'[new_item.icon_state]','[new_item.iconFile]','[src.name]');")

	DblClick()
		if(builtowner)
			usr << "This was built by [builtowner]."

	//building it
	LeftClick(mob/player/x, loc, cont, params)
		if(!builtowner)
			if(placeable)
				if(x.buildmode!="Building" && x.buildmode!="Copy")
					x.buildmode="Building"
					x << "<font color=purple>Auto-switch to Build Mode.</font>"
				if(x.buildmode=="Building")
					var/icon/I=new(icon,icon_state)
					x.current_item=path
					x.client.mouse_pointer_icon=I
		else if(x.buildmode=="Copy")
			var/obj/builditems/b = new type(name=x.cur_name, iconstate=x.cur_state, iconfile=x.cur_file)
			x.clipboard.Add(b)
			x << "<font color=purple>Copied [src].</font>"
			for(var/o in vars)
				if(o=="path" || o!="uid" && o!="loc" && o!="saveable" && !(islist(vars[o])) && !(ispath(vars[o])))
					b.vars[o]=vars[o]
			b.builtowner=null
			b.placeable=TRUE
		else
			update(x)
		x.cur_name=null
		x.cur_file=null
		x.cur_state=null

	proc
		deletion()

			Query("DELETE FROM `[sql_objecttable]` WHERE uid=[uid];")

			del src

		save(mode,modifier)

			Query("UPDATE `[sql_objecttable]` SET [mode=="Infra Luminosity" ? "infra_luminosity" : "[ckey(mode)]"]=[modifier] WHERE uid=[uid];")

		update(mob/x)
			if(x.ckey!=builtowner)return

			if(builtowner&&x.buildmode=="Building")
				if(x.current_item)new x.current_item(loc,owner=x.ckey)
			else
				x.client.mouse_pointer_icon=null
				var
					change
					mode
				switch(x.buildmode)
					if("Layer")
						if(x.ckey==builtowner)
							switch(input(x,"What layer would you like it on?")in null|list("Mob level","Below Mobs","Above Mobs","Cancel"))
								if("Mob level") layer=MOB_LAYER
								if("Below Mobs")layer=MOB_LAYER-1
								if("Above Mobs")layer=MOB_LAYER+1
							change=layer
						else x << "<font color=purple>That is not yours."
					if("Opacity")
						if(x.ckey==builtowner)
							opacity=!opacity
							change=opacity
						else x << "<font color=purple>That is not yours."
					if("Luminosity")
						if(x.ckey==builtowner)
							var/i=input(x,"How much light should it give off? (in tiles)")as null|num
							luminosity=i
							change=luminosity
						else x << "<font color=purple>That is not yours."
					if("Infra Luminosity")
						if(usr.ckey==builtowner)
							var/i=input(x,"How visible is it in the dark? (in tiles)")as null|num
							infra_luminosity=i
							change=infra_luminosity
						else x << "<font color=purple>That is not yours."
					if("Density")
						if(x.ckey==builtowner)
							density=!density
							change=density
						else x << "<font color=purple>That is not yours."
					if("Deletion")
						if(x.ckey==builtowner)
							Query("DELETE FROM `[sql_objecttable]` WHERE uid=[uid];")
							del src
						else x << "<font color=purple>That is not yours."
					if("Delete Tiles")
						for(var/obj/builditems/o in loc)
							if(o.builtowner==x.ckey)

								Query("DELETE FROM `[sql_objecttable]` WHERE uid=[o.uid];")

								del o
							else
								x << "[o] does not belong to you."
								return
				if(!mode)mode=x.buildmode
				save(mode,change)


turf
	unbuildable
		icon='BuildTwo.dmi'

		LeftClick(cal, loc, cont, params)
			var/mob/x=usr
			if(x.buildmode!="Normal")
				if(x.buildmode=="Building"&&x.current_item)usr << "<font color=purple>You cannot build here.</font>"
			else return ..()

	buildable
		Entered()
			..()
			if(usr.client)
				var/mob/x=usr
				winset(x,"main.showobjects","is-disabled=false")
			//	winset(x,"main.build","is-disabled=false")

		icon='BuildTwo.dmi'
		icon_state="Ground"

		//building stuff
		//supports building from clipboard
		LeftClick(cal, loc, cont, params)
			var/mob/player/x=usr
			if(x.buildmode!="Normal")
				if(x.buildmode=="Building"&&x.current_item)
					var/obj/builditems/new_item=new x.current_item(loc=src,owner=x.ckey)
					new_item.loc=src
					new_item.iconFile=x.cur_file
					new_item.name=x.cur_name
					new_item.icon_state=x.cur_state
				else if(x.buildmode=="Copy"&&x.current_item)
					var/obj/builditems/new_item=new x.current_item(loc=src,owner=x.ckey)
					sleep(1)
					var/obj/builditems/b = locate(new_item.type) in x.clipboard
					if(!b)return
					for(var/o in b.vars)
						if(o!="builtowner" && o!="loc" && o!="x" && o!="y" && o!="z" && o!="uid" && !(islist(b.vars[o])) && !(ispath(b.vars[o])))new_item.vars[o]=b.vars[o]

	changeable
		//this is for the changeable worlds